package com.example.trueorfalse.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.trueorfalse.R;
import com.example.trueorfalse.model.Question;
import com.example.trueorfalse.tools.QuestionLab;


/**
 * 问答的详情页
 */
public class SecondActivity extends AppCompatActivity {
    private static final String TAG = "SecondActivity";
    private static final String EXTRA_TAG = "com.example.trueorfalse.msg";

    private ImageView mDemoImageView;

    private Question[] mQuestions;
    private Button mNextButton;
    private TextView mQuestionTextView;
    private EditText mAnswerEditText;
    private int currentIndex = 0;
    private  Button mCommitButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mQuestions = QuestionLab.get().getQuestions();
        loadLayout();
        loadAnswerText();
        loadNextButton();
        loadCommitButton();
    }

    public void loadLayout(){
        setContentView(R.layout.activity_second);
        mQuestionTextView = findViewById(R.id.question_text);//问题显示框
        mAnswerEditText = findViewById(R.id.answer_text);//回答编辑框
        mNextButton = findViewById(R.id.next_button);//下一个按钮
        mCommitButton = findViewById(R.id.commit_button);//提交发送按钮
        mDemoImageView = findViewById(R.id.image_view_demo1);//图片展示框
    }

    public void loadNextButton(){
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mQuestionTextView.setText(mQuestions[++currentIndex].getTextResId());
                mAnswerEditText.setText("");
                if(currentIndex == (mQuestions.length-1)){
                    mNextButton.setVisibility(View.INVISIBLE);
                    mCommitButton.setVisibility(View.VISIBLE);
                }
            }
        });
    }
    public void loadCommitButton(){
        mCommitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                QuestionLab.get().setQuestions(mQuestions);
                Intent intent = ListQuestionActivity.newIntent(SecondActivity.this, "结果展示");
                startActivity(intent);
                finish();
            }
        });
    }
    public void loadAnswerText(){
        mAnswerEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mQuestions[currentIndex].setAnswer(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    public static Intent newIntent(Context context, String data){
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra(EXTRA_TAG,data);
        return intent;
    }

}











